const app = getApp();
import Toast from '../../../vant/toast/toast';
import {
  collectionStatistic,
  collectionInfo
} from '../../../utils/api/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isIPhoneX: app.globalData.isIPhoneX,
    navHeight: app.globalData.statusHeight, //顶部导航栏高度
    isLogin: true,
    collectionList: [],
    collectionData: {
      collectionName: "",
      collectNum: 0,
      orderNum: 0,
      commission: 0
    },
    statisticalDateSort: 'DESC',
    totalNum: 0,
    pageNo: 1,
    pageSize: 20,
    isAll: false,
    pageVisible: false
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      collectionList: [],
      pageNo: 1,
      isAll: false,
      pageVisible: false
    });
    this.getInfo()
    this.getCollectionStatistic()
    const storage = wx.getStorageSync('loginInfo');
    this.setData({
      isLogin: storage ? true : false,
    });
  },

  onReachBottom: function () {
    if (this.data.pageVisible === false) {
      if (this.data.pageNo * this.data.pageSize < this.data.totalNum) {
        let nowPageNo = this.data.pageNo + 1
        this.setData({
          pageVisible: false,
          pageNo: nowPageNo
        })
        this.getCollectionStatistic()
      } else {
        this.setData({
          pageVisible: false
        })
        wx.showToast({
          icon: 'none',
          title: '没有更多了',
        })
      }
    }

  },

  back () {
    wx.navigateBack({
      delta: 1
    })
  },

  getInfo () {
    collectionInfo().then(res => {
      if (res.resultStatus) {
        this.setData({
          collectionData: {
            collectionName: res.resultData.collectionName,
            collectNum: res.resultData.collectNum,
            orderNum: res.resultData.orderNum,
            commission: res.resultData.commission
          }
        })
      }
    })
  },

  changeSort() {
    if (this.data.statisticalDateSort === 'DESC') {
      this.setData({
        statisticalDateSort: 'ASC',
        collectionList: [],
        pageNo: 1,
        isAll: false,
        pageVisible: false
      })
    } else {
      this.setData({
        statisticalDateSort: 'DESC',
        collectionList: [],
        pageNo: 1,
        isAll: false,
        pageVisible: false
      })
    }
    this.getCollectionStatistic()
  },

  getCollectionStatistic() {
    collectionStatistic({
      statisticalDateSort: this.data.statisticalDateSort,
      pageNo: this.data.pageNo,
      pageSize: this.data.pageSize
    }).then((res) => {
      this.setData({
        pageVisible: true
      })
      if (this.data.pageNo > 1) {} else {
        this.setData({
          collectionList: []
        })
      }
      if (res.resultStatus) {
        const data = res.resultData;
        let nowdataList = []
        this.setData({
          totalNum: data.totalNum
        });
        if (data.dataList.length) {
          if (this.data.pageNo > 1) {
            nowdataList = this.data.collectionList.concat(data.dataList);
          } else {
            nowdataList = data.dataList
          }
          this.setData({
            collectionList: nowdataList
          });
        } else {
          this.setData({
            isAll: true
          });
        }
        this.setData({
          pageVisible: false,
        })
      } else {
        this.setData({
          pageVisible: false,
        })
        Toast(res.errorMessage);
      }
    });
  }
})